package com.example.demo.common;

import lombok.Data;

@Data
public class AjaxResult {

    private Integer code;
    private String msg;
    private Object data;

    /**
     * 成功返回数据格式
     * @param msg
     * @param data
     * @return
     */
    public static AjaxResult success(String msg, Object data) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setCode(200);
        ajaxResult.setMsg(msg);
        ajaxResult.setData(data);
        return ajaxResult;
    }

    public static AjaxResult success(Object data) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setCode(200);
        ajaxResult.setMsg("");
        ajaxResult.setData(data);
        return ajaxResult;
    }

    /**
     * 返回失败的结果
     * @param code
     * @param msg
     * @param data
     * @return
     */
    public static AjaxResult error(Integer code, String msg, Object data) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setCode(code);
        ajaxResult.setMsg(msg);
        ajaxResult.setData(data);
        return ajaxResult;
    }

    public static AjaxResult error(String msg, Object data) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setCode(403);
        ajaxResult.setMsg(msg);
        ajaxResult.setData(data);
        return ajaxResult;
    }

    public static AjaxResult error(Object data) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setCode(403);
        ajaxResult.setMsg("");
        ajaxResult.setData(data);
        return ajaxResult;
    }
}
